package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.DwAddHistoryDTO;
import com.xbongbong.paas.pojo.dto.DwDelLogDTO;
import com.xbongbong.paas.pojo.vo.DwDelLogVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.service.DataWarningHistoryService;
import com.xbongbong.pro.statistic.service.DataWarningStrategyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @ClassName DataWarningScript
 * @Description 数据预警
 * @Author mengna.Deng
 * @Date 2021/6/4 16:52
 * @Version 1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart/dataWarning")
public class DataWarningScript {
    private static final Logger LOG = LoggerFactory.getLogger(DataWarningScript.class);
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private DataWarningHistoryService dataWarningHistoryService;
    @Resource
    private DataWarningStrategyService dataWarningStrategyService;

    /**
     * 数据预警日志删除（一个月前的）
     * @param dwDelLogDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delLog", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delLog(@RequestBody @Valid DwDelLogDTO dwDelLogDTO, BindingResult br) throws Exception {
        XbbResponse<DwDelLogVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if(!proBaseConfig.getXbbCode().equals(dwDelLogDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                DwDelLogVO dwDelLogVO = dataWarningStrategyService.batchPhysicalDelete(dwDelLogDTO);
                response = new XbbResponse<>(dwDelLogVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 预警历史定时产生（在BI数据同步，推迟一小时后）
     */
    @RequestMapping(value = "/addHistoryData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addHistoryData(@RequestBody @Valid DwAddHistoryDTO dwAddHistoryDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if(!proBaseConfig.getXbbCode().equals(dwAddHistoryDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                dataWarningStrategyService.dataWarningStrategy(dwAddHistoryDTO);
                response = new XbbResponse<>();
            }catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
